package com.smartalarmclock.UI;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.smartalarmclock.UI.R;

public class HistoryActivity extends Activity {
	
	public final static String EXTRA_MESSAGE = "com.smartalarmclocklocal.ui.uiimpl.MESSAGE";
	private ArrayList<LogListItem> m_ListLogs = new ArrayList<LogListItem>();
	private ListView m_ListViewLog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v("History Activity", "Start");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.log_list);
		
		m_ListLogs = GetSearchResults();

		
        // set listener for buttons
        final Button clearLogButton = (Button)findViewById(R.id.buttonClearSelectedLogs);
        clearLogButton.setOnClickListener(clearLogListener);
		
        // set profile list adapter and items on-click listener
        m_ListViewLog = (ListView) findViewById(R.id.log_list);
        m_ListViewLog.setAdapter(new LogListAdapter(this, m_ListLogs));
        m_ListViewLog.setOnItemClickListener(logListItemClickListener);
	}
	
	
    @Override
    protected void onResume()
    {
    	super.onResume();
    	// update the active profiles from the AcManager
  		final ListView logListView = (ListView) findViewById(R.id.log_list);
        ArrayAdapter<LogListItem> arrayAdapter = (ArrayAdapter<LogListItem>)logListView.getAdapter();
		arrayAdapter.notifyDataSetChanged();
    }
	
	
	private OnItemClickListener logListItemClickListener = new OnItemClickListener() {
		@Override
    	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
		    String logMessage = m_ListLogs.get(position).getLogMsg();
		    Intent logDetailsintent = new Intent(HistoryActivity.this, LogItemDetailsActivity.class);
		    logDetailsintent.putExtra(EXTRA_MESSAGE, logMessage); 
    		Log.v("startActivity(logDetailsintent);", "Start");
      		startActivity(logDetailsintent);
    		Log.v("startActivity(logDetailsintent);", "Finish");
      		
    		/*Object o = m_ListViewLog.getItemAtPosition(position);
        	LogListItem obj_itemDetails = (LogListItem)o;
    		Toast.makeText(HistoryActivity.this, "You have chosen : " + " " 
        	+ obj_itemDetails.getLogMsg(), Toast.LENGTH_LONG).show();*/
		}
	};
 
		
	private OnClickListener clearLogListener = new OnClickListener()
	{
    	@Override
        public void onClick(View v)
        {   
    		ArrayList<LogListItem> tmpListLogs = new ArrayList<LogListItem>();
    		tmpListLogs = (ArrayList<LogListItem>) m_ListLogs.clone();
    		StringBuffer responseText = new StringBuffer();  
			 responseText.append("The following were deleted...\n"); 
			 for(LogListItem log: m_ListLogs){    
				 if(log.isSelected()){   
					 responseText.append("\n" + log.getName()); 
					 tmpListLogs.remove(log);
					 }  
				 }   
			 Toast.makeText(getApplicationContext(), responseText, Toast.LENGTH_LONG).show();
			 m_ListLogs.clear();
			 m_ListLogs = (ArrayList<LogListItem>) tmpListLogs.clone();
			 refresh();
		 }
    };
    
    private void refresh()
    {
  		final ListView logListView = (ListView) findViewById(R.id.log_list);
        ArrayAdapter<LogListItem> arrayAdapter = (ArrayAdapter<LogListItem>)logListView.getAdapter();
		arrayAdapter.notifyDataSetChanged();
    }
    


    
	private ArrayList<LogListItem> GetSearchResults() {
		final ArrayList<LogListItem> results = new ArrayList<LogListItem>();
		Calendar calender = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = dateFormat.format(calender.getTime());
        String lastUse = "Last use: ";

		
		LogListItem item_details = new LogListItem("Family", lastUse + formattedDate);
		item_details.setLogMsg("dsfsdfsdgsdgsdfg/nasdfasdfasdfadsdfasfas/n/n/n/n/n/ndsfasdfasfasfas" +
				"fddasfasfasfasfasdfa/nafddsadfasfasfdadsafasfdasfasfasfasfd/" +
				"nasdfffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"fffffffffffffff/n55555555" +
				"dsfsdfsdgsdgsdfg/nasdfasdfasdfadsdfasfas/n/n/n/n/n/ndsfasdfasfasfas" +
				"fddasfasfasfasfasdfa/nafddsadfasfasfdadsafasfdasfasfasfasfd/" +
				"nasdfffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"dsfsdfsdgsdgsdfg/nasdfasdfasdfadsdfasfas/n/n/n/n/n/ndsfasdfasfasfas" +
				"fddasfasfasfasfasdfa/nafddsadfasfasfdadsafasfdasfasfasfasfd/" +
				"nasdfffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"dsfsdfsdgsdgsdfg/nasdfasdfasdfadsdfasfas/n/n/n/n/n/ndsfasdfasfasfas" +
				"fddasfasfasfasfasdfa/nafddsadfasfasfdadsafasfdasfasfasfasfd/" +
				"nasdfffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"dsfsdfsdgsdgsdfg/nasdfasdfasdfadsdfasfas/n/n/n/n/n/ndsfasdfasfasfas" +
				"fddasfasfasfasfasdfa/nafddsadfasfasfdadsafasfdasfasfasfasfd/" +
				"nasdfffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"dsfsdfsdgsdgsdfg/nasdfasdfasdfadsdfasfas/n/n/n/n/n/ndsfasdfasfasfas" +
				"fddasfasfasfasfasdfa/nafddsadfasfasfdadsafasfdasfasfasfasfd/" +
				"nasdfffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"dsfsdfsdgsdgsdfg/nasdfasdfasdfadsdfasfas/n/n/n/n/n/ndsfasdfasfasfas" +
				"fddasfasfasfasfasdfa/nafddsadfasfasfdadsafasfdasfasfasfasfd/" +
				"nasdfffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
				"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    	results.add(item_details);
    	
    	formattedDate = dateFormat.format(calender.getTime());
    	item_details = new LogListItem("Friends", lastUse +formattedDate);
    	results.add(item_details);
    	
    	formattedDate = dateFormat.format(calender.getTime());
    	item_details = new LogListItem("Work", lastUse +formattedDate);
    	results.add(item_details);
    	
    	formattedDate = dateFormat.format(calender.getTime());
    	item_details = new LogListItem("Noon", lastUse + formattedDate);
    	results.add(item_details);
    	
    	formattedDate = dateFormat.format(calender.getTime());
    	item_details = new LogListItem("All", lastUse + formattedDate);
    	results.add(item_details);
    	
    	return results;
    }
	
}

